package com.ty.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.*;

/**
 * @author 天宇宇宇宇宇
 * @Data 2020/12/1 19:58
 *
 * SpringMVC配置类
 */
@EnableWebMvc
@Configuration
@ComponentScan(basePackages = {"com.ty"}, includeFilters = {
        @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Controller.class, RestController.class})
}, useDefaultFilters = false)
public class MvcConfig implements WebMvcConfigurer {

    /**
     * 视图解析器
     */
    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        registry.jsp("/views/",".html");
    }

    /**
     * 静态资源
     */
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    /**
     * 视图控制器
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        //初始页面
        registry.addViewController("/").setViewName("customerPhone/index");
        //用户登录页面
        registry.addViewController("/login").setViewName("customerPhone/html/login");
        //后台管理
        registry.addViewController("/admin").setViewName("customerService/back");
    }

    /**
     * 文件上传解析器
     * @return
     */
    @Bean
    public CommonsMultipartResolver multipartResolver(){
        CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver();
        //编码格式配置
        commonsMultipartResolver.setDefaultEncoding("utf-8");
        //设置上传的文件最大值,以字节为单位,-1代表无限制
        commonsMultipartResolver.setMaxUploadSize(-1);
        return  commonsMultipartResolver;
    }


}
